<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/4/15
 * Time: 17:06
 */
error_reporting(0);
date_default_timezone_set('Asia/Shanghai');
require_once 'JSON.php';
if($_SERVER['DOCUMENT_ROOT']){
    $root_dir = $_SERVER['DOCUMENT_ROOT'];
}else{
    $root_dir = dirname(dirname(dirname(__FILE__)));
}

($_GET['path']) and ($file_path = $_GET['path']);
($_POST['path']) and ($file_path = $_POST['path']);

if(file_exists($path = str_replace('//','/',$root_dir.'/'.$file_path))){
    $file_dir = dirname($path);
    if(@unlink($path)){
        //如果目录为空就删除目录
        if ($f_dir=opendir($file_dir)){
            if(count(scandir($file_dir))<3){
                rmdir($file_dir);
            }
//            while (false !== ($file_list_name = readdir($f_dir))){
//                if(count(scandir($file_dir))<2){
//                    rmdir($file_dir);
//                    break;
//                }
//                if(in_array($file_list_name,['.','..'])){ continue;}
//            }
            closedir($f_dir);
        }

        return_json([
            'relative_path' => $file_path,
            //'absolute_path' => $path,
            'file_dir'      => $file_dir,
        ]);
    }
}
return_json([
    'relative_path' => $file_path,
    //'absolute_path' => $path,
],'删除失败，该文件不存在！',0);

function return_json($data,$msg='删除成功',$code=1) {
    header('Content-type: text/html; charset=UTF-8');
    $json = new Services_JSON();
    //echo $json->encode(array('error' => 1, 'message' => $msg));
    $re = [
        'code'      => $code,
        'message'   => $msg,
        'data'      => $data,
        'time'      => time(),
    ];
    exit($json->encode($re));
}